﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Z16.BLI
{
    public class Camada
    {
        #region "Variaveis"

        private string strNomeCamada;
        public string StrNomeCamada
        {
            get { return strNomeCamada; }
            set { strNomeCamada = value; }
        }

        #endregion

        #region "Metodos"

        //public int incCamada(string strNomeCamada, int? codCamada)
        //{
        //    try
        //    {
        //        if (codCamada == null)
        //        {
        //            SqlParameter[] pms =
        //            {
        //                new SqlParameter("@NomeCamada", strNomeCamada)
        //            };

        //            var returnCodCamada = SqlHelper.ExecuteNonQuery(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procIncCamada", pms);

        //            return returnCodCamada;
        //        }
        //        else
        //        {
        //            SqlParameter[] pms =
        //            {
        //                new SqlParameter("@Camada", strNomeCamada),
        //                new SqlParameter("@CodCamada", codCamada)
        //            };

        //            SqlHelper.ExecuteNonQuery(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procEditCamada", pms);

        //            return 0;//"0;Camada editada com sucesso!";
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return 99999; //"1;Erro ao inserir Camada: " + ex.Message;
        //    }
        //}

        public string incCamada(string strNomeCamada, int? codCamada)
        {
            try
            {
                if (codCamada == null)
                {
                    SqlParameter[] pms =
                    {
                        new SqlParameter("@NomeCamada", strNomeCamada)
                    };

                    var returnCodCamada = SqlHelper.ExecuteNonQuery(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procIncCamada", pms);

                    return "0;Camada adicionada com sucesso!";
                }
                else
                {
                    SqlParameter[] pms =
                    {
                        new SqlParameter("@Camada", strNomeCamada),
                        new SqlParameter("@CodCamada", codCamada)
                    };

                    SqlHelper.ExecuteNonQuery(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procEditCamada", pms);

                    return "0;Camada editada com sucesso!";
                }
            }
            catch (Exception ex)
            {
                return "1;Erro ao inserir Camada: " + ex.Message;
            }
        }

        public DataTable ListarCamadas(int? codCamada,string nome)
        {
            DataTable cc = new DataTable();

            SqlParameter[] pms =
                    {
                        new SqlParameter("@codCamada",codCamada),
                        new SqlParameter("@nome",nome),
                    };

            try
            {
                SqlDataReader retorno = SqlHelper.ExecuteReader(SqlHelper.GetSQLConnection(), CommandType.StoredProcedure, "procSelCamada", pms);
              
                cc.Load(retorno);
            }
            catch (Exception ex)
            {
                throw;
            }
            return cc;
        }

        public int ExcluirCamada(int codigo)
        {
            SqlConnection conn = new SqlConnection(SqlHelper.GetSQLConnection());
            conn.Open();
            SqlCommand dCmd = new SqlCommand("DeletarCamada", conn);
            dCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dCmd.Parameters.AddWithValue("@codigo", codigo);
                return dCmd.ExecuteNonQuery();
            }
            catch
            {
                throw;
            }
            finally
            {
                dCmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

        #endregion
    }
}